import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/home/index',
    name: 'Root',
    meta: {
      hidden: true,
    
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/service',
    component: () => import('@/views/Login/components/service.vue'),
    name: 'Service',
    meta: {
      hidden: true,
      title: "用户协议",
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/home',
    component: Layout,
    redirect: '/home/index',
    name: 'Home',
    meta: {},
    children: [
      {
        path: 'Index',
        name: 'index',
        component: () => import('@/views/Home/index.vue'),
        meta: {
          title: "工作台",
          icon: 'svg-icon:shouye1',
        }
      }
    ]
  },
  // {
  //   path: '/Personnel',
  //   component: Layout,
  //   redirect: '/Personnel/index',
  //   name: 'Personnel',
  //   meta: {},
  //   children: [
  //     {
  //       path: 'Personnel-index',
  //       name: 'personnel-index',
  //       component: () => import('@/views/Personnel/index.vue'),
  //       meta: {
  //         title: "人才推送",
  //         icon: 'svg-icon:personelIcon'
  //       }
  //     }
  //   ]
  // },
  {
    path: '/Recruit',
    component: Layout,
    redirect: '/Recruit/index',
    name: 'Recruit',
    meta: {},
    children: [
      {
        path: 'Recruit-index',
        name: 'recruit-index',
        component: () => import('@/views/Recruit/index.vue'),
        meta: {
          title: "招聘职位",
          icon: 'svg-icon:testbefores1'
        }
      }
    ]
  },
  // {
  //   path: '/Talent',
  //   component: Layout,
  //   redirect: '/Talent/index',
  //   name: 'Talent',
  //   meta: {},
  //   children: [
  //     {
  //       path: 'Talent-index',
  //       name: 'talent-index',
  //       component: () => import('@/views/Talent/index.vue'),
  //       meta: {
  //         title: "人才库",
  //         icon: 'svg-icon:testbefores2'
  //       }
  //     }
  //   ]
  // },
  {
    path: '/Data',
    component: Layout,
    redirect: '/Data/index',
    name: 'Data',
    meta: {},
    children: [
      {
        path: 'Data-index',
        name: 'data-index',
        component: () => import('@/views/Data/index.vue'),
        meta: {
          title: "数据统计",
          icon: 'svg-icon:testbefores3'
        }
      }
    ]
  },
  {
    path: '/Organizational',
    component: Layout,
    redirect: '/Organizational/index',
    name: 'Organizational',
    meta: {},
    children: [
      {
        path: 'Organizational-index',
        name: 'organizational-index',
        component: () => import('@/views/Organizational/index.vue'),
        meta: {
          title: "组织架构",
          icon: 'svg-icon:testbefores4'
        }
      }
    ]
  },
  {
    path: '/Admin',
    component: Layout,
    redirect: '/Admin/index',
    name: 'Admin',
    meta: {},
    children: [
      {
        path: 'Admin-index',
        name: 'admin-index',
        component: () => import('@/views/Admin/index.vue'),
        meta: {
          title: "用户管理",
          icon: 'svg-icon:testbefores8'
        }
      }
    ]
  },
  {
    path: '/Notice',
    component: Layout,
    redirect: '/Notice/index',
    name: 'Notice',
    meta: {},
    children: [
      {
        path: 'Notice-index',
        name: 'notice-index',
        component: () => import('@/views/Notice/index.vue'),
        meta: {
          title: "公司公告",
          icon: 'svg-icon:testbefores'
        }
      }
    ]
  },
  // {
  //   path: '/Resume',
  //   component: Layout,
  //   redirect: '/Resume/index',
  //   name: 'Resume',
  //   meta: {},
  //   children: [
  //     {
  //       path: 'Resume-index',
  //       name: 'resume-index',
  //       component: () => import('@/views/Resume/index.vue'),
  //       meta: {
  //         title: "应聘报名表",
  //         icon: 'svg-icon:testbefores6'
  //       }
  //     }
  //   ]
  // },

  {
    path: '/Message',
    component: Layout,
    redirect: '/Message/index',
    name: 'Message',
    meta: {},
    children: [
      {
        path: 'Message-index',
        name: 'message-index',
        component: () => import('@/views/Message/index.vue'),
        meta: {
          title: "短信管理",
          icon: 'svg-icon:testbefores5'
        }
      }
    ]
  },
  {
    path: '/business',
    component: Layout,
    redirect: '/Business/index',
    name: 'Business',
    meta: {},
    children: [
      {
        path: 'Business-index',
        name: 'business-index',
        component: () => import('@/views/Business/index.vue'),
        meta: {
          title: "企业设置",
          icon: 'svg-icon:teststart'
        }
      }
    ]
  },
  {
    path: '/Detail',
    component: Layout,
    redirect: '/Detail/index',
    name: 'Detail',
    meta: {
      hidden: true,
      noTagsView: true,
      noCache: true,
      canTo: true,
    },
    children: [
      {
        path: 'Detail-index',
        name: 'detail-index',
        component: () => import('@/views/Detail/index.vue'),
        meta: {
          hidden: true,
          noTagsView: true,
          noCache: true,
          canTo: true,
          title: "简历详情",
          icon: 'svg-icon:testbefores'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
